package com.tomowork.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
public class PageController {
    @RequestMapping("/")
    public String forwardIndex(HttpServletRequest request){
        return "index" ;
    }
    //跳转到用户登录页面
    @RequestMapping(value = "/loginPre",method = RequestMethod.GET)
    public String login(HttpServletRequest request){ //跳转到登录页面
        ModelAndView mv = null ;
        if(SecurityUtils.getSubject().isAuthenticated()){
            return "redirect:/" ;
        }else{
           return "login" ;
        }
    }
    //用户登出
    @RequestMapping("/logout2")
    public String logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "redirect:loginPre" ;
    }

    //跳转到用户注册页面
    @RequestMapping("/registPre")
    public String regist(){
        return "regist" ;
    }

    @RequestMapping("/testShiro")
    @RequiresPermissions(value = "appuser:getPersonalsss",logical = Logical.AND) // Logical.OR
    public String shiroTest(Map<String,Object> map){
//        Subject subject = SecurityUtils.getSubject();
//        boolean permitted = subject.isPermitted("appuser:del");
        map.put("permitted","看到这个说明有权限") ;
        return "testShiro" ;
    }
}
